/**
  ******************************************************************************
  * File Name          : prodrt.c
  * Description        : 实时产量数据上传
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "prodrt.h"
#include "gv.h"
#include <string.h>

#define _0d1g2t(x)     ((double)x/10000000.0)

static const char prodrt_data_format1[] = "产量监测系统\r\n市名:%02d;\r\n县名:%02d;\r\n矿名:%03d;\r\n";   /* 市名;县名;矿名 */
static const char prodrt_data_format2[] = "时间:%u-%02u-%02u/%02u:%02u:%02u;\r\n";                  /* 年-月-日/时:分:秒 */
static const char prodrt_data_format3[] = "版本:XK3208-A6 RevSZCL6.03;\r\n";                            /* 小时产量;小时产能比;实时产量 */
static const char prodrt_data_format4[] = "流量:%.3ft/h;\r\n带速:%.3fm/s;\r\n总产量:%.3ft;\r\n";    /* 日产量;月产量;年产量 */
static const char prodrt_data_format5[] = "年产量:%.3ft;\r\n月产量:%.3ft;\r\n日产量:%.3ft;\r\n";    /* 流量;皮带速度;状态 */
static const char prodrt_data_format6[] = "小时产量:%.3ft;\r\n班次:%u-%02u-%02u 第%u班;\r\n班产量:%.3ft;\r\n状态:%04X;\r\n";    /* 流量;皮带速度;状态 */

/* 网络数据 组织 发送 接收 */
uint8_t prodrt_data_upload(Ethernet_SendQueStru *tbuf)
{
  uint16_t pos;
  
  if(tbuf == NULL)
  {
    return 1;
  }

  pos = 0;
  pos += _snprintf((char *)(tbuf->buf+pos), (ETHERNET_TXBUF_MAXLEN-pos), prodrt_data_format1, \
                                                                         gv.hr.meas_pa.prod_city_num, \
                                                                         gv.hr.meas_pa.prod_county_num, \
                                                                         gv.hr.meas_pa.prod_mines_num);
  if(pos >= ETHERNET_TXBUF_MAXLEN){
    return 2;
  }

  pos += _snprintf((char *)(tbuf->buf+pos), (ETHERNET_TXBUF_MAXLEN-pos), prodrt_data_format2, \
                                                                         sys_datatime.year+2000, sys_datatime.month,  sys_datatime.day, \
                                                                         sys_datatime.hour, sys_datatime.minute, sys_datatime.second);
  if(pos >= ETHERNET_TXBUF_MAXLEN){
    return 3;
  }
  
  pos += snprintf((char *)(tbuf->buf+pos), (ETHERNET_TXBUF_MAXLEN-pos), prodrt_data_format3);//, sys_version.sv
  if(pos >= ETHERNET_TXBUF_MAXLEN){
    return 4;
  }

  pos += _snprintf((char *)(tbuf->buf+pos), (ETHERNET_TXBUF_MAXLEN-pos), prodrt_data_format4, \
                                                                         gv.ir.meas_ca.flowrate_kgh/1000.0, \
                                                                         gv.ir.meas_ca.belt_speed_mms/1000.0, \
                                                                         _0d1g2t(gv.ir.meas_ca.total_acc_0d1g));
  if(pos >= ETHERNET_TXBUF_MAXLEN){
    return 5;
  }

  pos += _snprintf((char *)(tbuf->buf+pos), (ETHERNET_TXBUF_MAXLEN-pos), prodrt_data_format5, \
                                                                         _0d1g2t(gv.ir.meas_ca.year_acc_0d1g), \
                                                                         _0d1g2t(gv.ir.meas_ca.month_acc_0d1g), \
                                                                         _0d1g2t(gv.ir.meas_ca.day_acc_0d1g));
  if(pos >= ETHERNET_TXBUF_MAXLEN){
    return 6;
  }

  pos += _snprintf((char *)(tbuf->buf+pos), (ETHERNET_TXBUF_MAXLEN-pos), prodrt_data_format6, \
                                                                         _0d1g2t(gv.ir.meas_ca.hour_acc_0d1g), \
                                                                         sys_datatime.year+2000, sys_datatime.month,  sys_datatime.day, \
                                                                         gv.ir.meas_ca.current_class+1, \
                                                                         _0d1g2t(meas_get_current_class_prod(&gv)), \
                                                                         0xFE00);
  if(pos >= ETHERNET_TXBUF_MAXLEN){
    return 7;
  }

  tbuf->len = pos;
  ethif_tosend(tbuf);

  return 0;
}
